package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kcsj.domain.Warehouse;
import com.kcsj.service.WarehouseService;
import com.kcsj.util.Condition;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import com.kcsj.util.Pagination;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@Slf4j
@WebServlet("/warehouse.ctl")
public class WarehouseController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
        //将request body转换为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        try {
            //根据请求反序列化生成对象
            Warehouse warehouse = JSON.parseObject(req_jsonStr,Warehouse.class);
            //添加产品信息
            int id = WarehouseService.getInstance().add(warehouse);
            JSONObject addMessage = MessageHelper.addMessage("增加成功");
            response.getWriter().println(addMessage);
        } catch (SQLException e) {
            JSONObject addMessage = MessageHelper.dateBaseError();
            response.getWriter().println(addMessage);
            log.error(e.getMessage());
        } catch (Exception e) {
            JSONObject addMessage = MessageHelper.netWordError();
            //响应respMessage_jsonObj到前端
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试
            log.error(e.getMessage());
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String request_jsonstr = JSONUtil.getJSON(request);
        try {
            Warehouse warehouse = JSON.parseObject(request_jsonstr, Warehouse.class);
            //到数据库表修改Degree对象对应的记录
            boolean judge = WarehouseService.getInstance().update(warehouse);
            JSONObject addmessage;
            if (judge) {
                addmessage = MessageHelper.addMessage("修改成功");
            }else {
                addmessage = MessageHelper.addMessage("修改异常");
            }
            response.getWriter().println(addmessage);
        } catch (SQLException e) {
            JSONObject addMessage = MessageHelper.dateBaseError();
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            JSONObject addMessage = MessageHelper.netWordError();
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从JSONObject对象中读取键“no”的值（Java Object对象），
        String no_obj = request.getParameter("warehouseNo");
        try {
            //到数据库表中删除对应的教师
            boolean judge = WarehouseService.getInstance().delete(no_obj);
            JSONObject addMessage;
            if (judge) {
                addMessage = MessageHelper.addMessage("删除成功");
            }else {
                addMessage = MessageHelper.addMessage("删除失败");
            }
            response.getWriter().println(addMessage);
        } catch (SQLException e) {
            JSONObject addMessage = MessageHelper.dateBaseError();
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            JSONObject addMessage = MessageHelper.netWordError();
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //从JSONObject对象中读取键“id”的值（Java Object对象），req_jsonObj可能是{"id":1}
        String no_obj = request.getParameter("warehouseNo");
        try {
            if (no_obj!=null) {
                this.responseWarehouse(response, no_obj);
            }else {
                this.responseWarehouse(response);
            }
        } catch (SQLException e) {
            JSONObject jsonObject = MessageHelper.dateBaseError();
            log.error(e.getMessage());
            //响应respMessage_jsonObj到前端
            response.getWriter().println(jsonObject);
        } catch (Exception e) {
            JSONObject netWordError = MessageHelper.netWordError();
            //响应respMessage_jsonObj到前端
            response.getWriter().println(netWordError);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
    }

    //响应单个配送员
    private void responseWarehouse(HttpServletResponse response, String no) throws SQLException, IOException {
        Warehouse warehouse = WarehouseService.getInstance().find(no);
        String deliveryPerson_jsonstr= JSON.toJSONString(warehouse);
        response.getWriter().println(deliveryPerson_jsonstr);
    }


    //响应多个配送员
    private void responseWarehouse(HttpServletResponse response) throws SQLException, IOException {
        Collection<Warehouse> warehouses = WarehouseService.getInstance().findAll();
        String productModels_jsonstr = JSON.toJSONString(warehouses);
        JSONObject data = MessageHelper.addData(productModels_jsonstr);
        //响应resp_jsonObj到前端
        response.getWriter().println(data);
    }
}
